package com.nuc.kl.module.kpoint.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.kpoint.entity.Knowledge;
import com.nuc.kl.module.kpoint.entity.Reference;

@Controller
@Scope("prototype")
public class KPointAction extends BaseAction<Knowledge> {

	private String ids;// 删除所用多个id
	private String findName;// 模糊查询所用
	private String rows;// 每页显示的记录数
	private String page;// 当前第几页
	private String indexType;//是查找的条件
	private String indexValue;//查找的值
	private String minPeriod;// 最小学时
	private String maxPeriod;// 最大学时
	private String order;// 是否排序
	private String sort;// 排序规则
	private String creater;// 创建者
	private String resentTime;// 最新更新时间
	private Long editTimes;// 编辑次数
	private String view;// 审核时专家的观点
	private String parentKIds;//父知识点ids
	private String preKIds;//前导知识点ids
	private String referenceIds;//参考资料ids

	/** 列表页面 */
	public String listUI() throws Exception {

		return "list";
	}

	/** 列表 */
	public void listJson() throws Exception {
		int intPage = Integer.parseInt((page == null || page == "0") ? "1"
				: page);
		int number = Integer.parseInt((rows == null || rows == "0") ? "10"
				: rows);
		int start = (intPage - 1) * number;

		List<Knowledge> list = null;
		// 与Dao层对应 实现按个别属性的搜索功能以及排序方式等
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("name", model.getName());
		m.put("minPeriod", minPeriod);
		m.put("maxPeriod", maxPeriod);
		m.put("description", model.getDescription());
		m.put("defficulty", model.getDefficulty());
		if(model.getNavigation() != null){
			m.put("navigation", model.getNavigation().getId());
		}
		// 如果没有传状态的值 返回正常显示和审核不通过的list
		if (model.getStatus() == 0) {
			m.put("status", "0,3");
		} else {
			m.put("status", model.getStatus());
		}
		m.put("order", order);
		m.put("sort", sort);

		list = kPointService.searchPageByIndex(start, number, m);

		// toBeJson(list, kPointService.count(model),new String[0]);

		// 设置要排除的属性 主要防止beanTOJson中的cycle错误
		String[] filterStr = { "nowledge", "parent", "navigation","references" };
		toBeJson(list, list.size(), filterStr);

	}

	/** 添加页面 */
	public String addUI() throws Exception {

		return "addUI";
	}

	/** 添加 */
	public void add() throws Exception {
		//插入前导知识点
		if(!preKIds.equals("")){
			String[] preKId = preKIds.split(",");
			Set<Knowledge> preKnowledges = new HashSet<Knowledge>();
			for (String str : preKId) {
				preKnowledges.add(kPointService.getById(Long.parseLong(str)));
			}
			model.setPreKnowledges(preKnowledges);
		}
		//插入父类知识点
		if(!parentKIds.equals("")){
			String[] parentKId = parentKIds.split(",");
			Set<Knowledge> parentKnowledges = new HashSet<Knowledge>();
			for (String str : parentKId) {
				parentKnowledges.add(kPointService.getById(Long.parseLong(str)));
			}
			model.setParentKnowledges(parentKnowledges);
		}
		//插入参考资料
		if(!referenceIds.equals("")){
			String[] referenceId = referenceIds.split(",");
			Set<Reference> References = new HashSet<Reference>();
			for (String str : referenceId) {
				References.add(referenceService.getById(Long.parseLong(str)));
			}
			model.setReferences(References);
		}
		
		try {
			kPointService.save(model, getCurrentUser());
			sendMessage("ok", "操作成功！");

		} catch (Exception e) {
			sendMessage("error", "失败，请重试！");
		}
	
		// 输出提示语句

	
	}

	/** 删除  没有真正删除 知识status改为4*/
	public void deleteByIds() throws Exception {

		try {
			// kPointService.deleteByIds(ids);
			String[] arrId = ids.split(",");
			for (String str : arrId) {
				kPointService.toReCycleBin(Long.parseLong(str), getCurrentUser());
			}
			sendMessage("ok", "操作成功");
		} catch (Exception e) {
			sendMessage("error", "操作失败，请重试！");
		}
	}
	
	/** 彻底删除 */
	public void delete() throws Exception {

		try {
			
			kPointService.deleteByIds(model.getId()+"");
			sendMessage("ok", "操作成功");
			
		} catch (Exception e) {
			sendMessage("error", "操作失败，请重试！");
		}
	}
	
	
	

	/** 编辑页面 */
	public String editUI() throws Exception {
		model = kPointService.getById(model.getId());
		model.setDescription(htmlspecialchars(model.getDescription()));
		return "editUI";
	}

	/** 修改 */
	public void edit() throws Exception {
		
		if(!parentKIds.equals("")){
			String[] parentKId = parentKIds.split(",");
			Set<Knowledge> parentKnowledges = new HashSet<Knowledge>();
			for (String str : parentKId) {
				parentKnowledges.add(kPointService.getById(Long.parseLong(str)));
				model.setParentKnowledges(parentKnowledges);
			}
		}
		if(!preKIds.equals("")){
			String[] preKId = preKIds.split(",");
			Set<Knowledge> preKnowledges = new HashSet<Knowledge>();
			for (String str : preKId) {
				preKnowledges.add(kPointService.getById(Long.parseLong(str)));
				model.setParentKnowledges(preKnowledges);
			}
			model.setPreKnowledges(preKnowledges);
		}
		if(!referenceIds.equals("")){
			String[] referenceId = referenceIds.split(",");
			Set<Reference> References = new HashSet<Reference>();
			for (String str : referenceId) {
				References.add(referenceService.getById(Long.parseLong(str)));
			}
			model.setReferences(References);
		}
		
		try {
			kPointService.update(model, getCurrentUser());
			sendMessage("ok", "更新成功！");
		
		} catch (Exception e) {
			sendMessage("error", "更新失败，请重试！");
		}
		
	}

	/** 审核页面 */
	public String checkUI() throws Exception {

		return "checkUI";
	}

	/** 回收站页面 */
	public String reCycleUI() throws Exception {

		return "reCycleUI";
	}
	
	/** 回收 */
	public void reCycle() throws Exception {
		model = kPointService.getById(model.getId());
		kPointService.reCycle(model, getCurrentUser());
		sendMessage("ok", "操作成功！");
	}

	/** 审核 */
	public void check() throws Exception {
		model = kPointService.getById(model.getId());
		knowledgeCheckService.check(model, view, getCurrentUser());

		sendMessage("ok", "操作成功！");
	}

	/** 知识点展示页面 */
	public String show() throws Exception {
		model = kPointService.getById(model.getId());
		creater = kPointService.getCreator(model.getId());
		editTimes = kPointService.editTimes(model.getId());
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		resentTime = dateFormat.format(kPointService.getResentTime(model.getId()));
		return "show";
	}

	public void loadByNav() throws Exception {

		List<Knowledge> list = kPointService.loadByNav(model.getId());
		String[] filterStr = { "parent", "nowledge", "navigation" };
		toBeJson(list, kPointService.count(model), filterStr);
	}

	/** 查询 */
	public String find() throws Exception {

		return "toList";
	}

	// -----------------getter-----------setter---------------------------

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getIndexType() {
		return indexType;
	}

	public void setIndexType(String indexType) {
		this.indexType = indexType;
	}

	public String getIndexValue() {
		return indexValue;
	}

	public void setIndexValue(String indexValue) {
		this.indexValue = indexValue;
	}

	public String getMinPeriod() {
		return minPeriod;
	}

	public void setMinPeriod(String minPeriod) {
		this.minPeriod = minPeriod;
	}

	public String getMaxPeriod() {
		return maxPeriod;
	}

	public void setMaxPeriod(String maxPeriod) {
		this.maxPeriod = maxPeriod;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}

	public Long getEditTimes() {
		return editTimes;
	}

	public void setEditTimes(Long editTimes) {
		this.editTimes = editTimes;
	}

	public String getResentTime() {
		return resentTime;
	}

	public void setResentTime(String resentTime) {
		this.resentTime = resentTime;
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	private String htmlspecialchars(String str) {
		str = str.replaceAll("&", "&amp;");
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "&quot;");
		return str;
	}

	public String getParentKIds() {
		return parentKIds;
	}

	public void setParentKIds(String parentKIds) {
		this.parentKIds = parentKIds;
	}

	public String getPreKIds() {
		return preKIds;
	}

	public void setPreKIds(String preKIds) {
		this.preKIds = preKIds;
	}

	public String getReferenceIds() {
		return referenceIds;
	}

	public void setReferenceIds(String referenceIds) {
		this.referenceIds = referenceIds;
	}
	
}
